<?php
	/**
	 * 内容管理类
	 * detail description
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	class html
	{
		var $database = null;

		function html()
		{
			$this->database = load( 'database' );
		}


		//返回配置内容列表数据
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 12;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$order = isset( $data['order'] ) ? $data['order'] : ' html.id desc';
			$cate_id = isset( $data['cate_id'] ) ? $data['cate_id'] : '';
			$audit_flag = isset( $data['audit_flag'] ) ? $data['audit_flag'] : '';
			$title = isset( $data['title'] ) ? $data['title'] : '';
			$saved = isset( $data['saved'] ) && preg_match( '/^\d+/', $data['saved'] ) ? $data['saved'] : -1;	 //是否获取生成了html的记录 1是0否
			$ext_table = isset( $data['ext_table'] ) ? $data['ext_table'] : '';
			$home_promote_flag = isset( $data['home_promote'] ) ? $data['home_promote'] : 0;
			$node_promote_flag = isset( $data['node_promote'] ) ? $data['node_promote'] : 0;
			$list_promote_flag = isset( $data['list_promote'] ) ? $data['list_promote'] : 0;

			$where = ' where html.create_account_id=account.id and html.status=0 ' . ( ! empty( $ext_table ) ? ' and html_' . $ext_table . '.html_id=html.id ' : '' );
			if ( $cate_id != '' )
			{
				if ( strpos( $cate_id, ',' ) > 0  )
				{
					$where .= ' and html.cate_id in ( ' . $cate_id . ' ) ';
				}
				else
				{
					$where .= ' and html.cate_id=' . $cate_id;
				}
			}
			if ( $audit_flag != '' )
			{
				$where .= ' and html.audit_flag=' . $audit_flag;
			}
			if ( $title != '' )
			{
				$where .= " and html.title_short like '%" . $title . "%' ";
			}
			if ( $saved != -1 )
			{
				$where .= " and html.save_path " . ( $saved == 1 ? '<>' : '=' ) . " '' ";
			}
			if ( $home_promote_flag == 1 )
			{
				$where .= " and html.home_promote_flag = 1 ";
				$order = ' html.home_order_num desc';
			}
			if ( $node_promote_flag == 1 )
			{
				$where .= " and html.node_promote_flag = 1 ";
				$order = ' html.node_order_num desc';
			}
			if ( $list_promote_flag == 1 )
			{
				$where .= " and html.list_promote_flag = 1 ";
				$order = ' html.list_order_num desc';
			}
			$count = 'from html,account' . ( ! empty( $ext_table ) ? ', html_' . $ext_table : '' ) . $where;
			$list = 'select html.*,account.username ' . ( ! empty( $ext_table ) ? ',html_' . $ext_table . '.*' : '' ) . $count . ' order by ' . $order;

			$result = $this->database->split( $count, $list, $size, $page );

			return $result;
		}

		//获取具体配置的数据
		function get( $data, $ext_table='' )
		{
			$result = array();
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				if ( $ext_table == '' )
				{
					$result = $this->database->unique( 'select * from html where status = 0 and id = ' . $data['id'] );
				}
				else
				{
					$result = $this->database->unique( 'select * from html,html_' . $ext_table . ' where html.id=html_' . $ext_table . '.html_id and html.id = ' . $data['id'] . ' and html.status = 0 ' );
				}
			}

			return $result;
		}

		//修改具体配置的数据
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['id']['kind'] = '';
			$field['id']['type'] = 'num';
			$field['id']['min'] = '1';
			$field['id']['max'] = '8';
			$field['id']['info'] = '请传递ID';

			$field['cate_id']['kind'] = '';
			$field['cate_id']['type'] = 'num';
			$field['cate_id']['min'] = '1';
			$field['cate_id']['max'] = '8';
			$field['cate_id']['info'] = '请传递栏目ID';

			$field['title_short']['kind'] = '';
			$field['title_short']['type'] = 'text';
			$field['title_short']['min'] = '1';
			$field['title_short']['max'] = '100';
			$field['title_short']['info'] = '请传递短标题';

			$field['title_short']['kind'] = '';
			$field['title_full']['type'] = 'text';
			$field['title_full']['min'] = '1';
			$field['title_full']['max'] = '255';
			$field['title_full']['info'] = '请传递长标题';

			$field['keyword']['kind'] = '';
			$field['keyword']['type'] = 'text';
			$field['keyword']['min'] = '1';
			$field['keyword']['max'] = '255';
			$field['keyword']['info'] = '请传递关键词';

			$field['fake_click']['kind'] = '';
			$field['fake_click']['type'] = 'num';
			$field['fake_click']['min'] = '1';
			$field['fake_click']['max'] = '9999';
			$field['fake_click']['info'] = '请传递假点击数';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				//上传预览图
				$templateurl = config( 'dir.physical_upload' );
				$FILE_NAME_STYLE = 'datetime';
				$upload = load( 'upload' );
				$upload_config = array( 'DEST_DIR' => $templateurl, 'FILE_TYPE' => 'jpg,png,gif',  'MUST_UPLOAD_FILE_INDEX' => 0 );
				$upload->init_cfg( array( 'preview_img_path' =>  $upload_config, 'photo' => $upload_config ) );
				$upfile = $upload->do_upload();

				//构造添加到html表的数据
				$html_data = array();
				if ( isset( $upfile['preview_img_path'] ) && count( $upfile['preview_img_path'] ) > 0 )
				{
					$html_data['preview_img_path'] = substr( $upfile['preview_img_path'][0], strlen( config( 'dir.tpl.root' ) ) - 1 );
				}
				$html_data['cate_id'] = $data['cate_id'];
				$html_data['title_short'] = $data['title_short'];
				$html_data['title_full'] = $data['title_full'];
				$html_data['title_short_color'] = $data['title_short_color'];
				$html_data['keyword'] = $data['keyword'];
				$html_data['keyword'] = $data['keyword'];
				$html_data['fake_click'] = $data['fake_click'];
				$html_data['home_promote_flag'] = ( isset( $data['home_promote_flag'] ) && $data['home_promote_flag'] == 'on' ? 1 : 0 );
				$html_data['home_order_num'] = $data['home_order_num'];
				$html_data['node_promote_flag'] = ( isset( $data['node_promote_flag'] ) && $data['node_promote_flag'] == 'on'  ? 1 : 0 );
				$html_data['node_order_num'] = $data['node_order_num'];
				$html_data['list_promote_flag'] = ( isset( $data['list_promote_flag'] ) && $data['list_promote_flag'] == 'on'  ? 1 : 0 );
				$html_data['list_order_num'] = $data['list_order_num'];
				$html_data['desc'] = $data['desc'];
				$html_data['modify_time'] = time();
				$html_data['modify_account_id'] = $_SESSION['user_info']['account_id'];
				$id = $this->database->set( 'html', $html_data, array( 'id' => $data['id'] ) );

				if ( preg_match( '!^\d+$!', $id ) && $this->database->end() )
				{
					$data['upload_files'] = $upfile;

					$argument = load( 'biz.argument' );
					$cate = load( 'biz.cate' );
					$cate_data = $cate->get( array( 'id'=>$data['cate_id'] ) );
					switch ( $cate_data['module_type_id'] )
					{
						case $argument->CM_NEWS:
							$this->set_news( $data );
							break;

						case $argument->CM_PHOTO:
							$this->set_photo( $data );
							break;

						case $argument->CM_VIDEO:
							$this->set_video( $data );
							break;

							default:
								$this->set_news( $data );
					}

					$result['success'] = true;
					$result['data']['id'] = $id;
				}
				else
				{
					$result['message'][] = '执行创建失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//添加新的配置内容
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['cate_id']['kind'] = '';
			$field['cate_id']['type'] = 'num';
			$field['cate_id']['min'] = '1';
			$field['cate_id']['max'] = '8';
			$field['cate_id']['info'] = '请传递栏目ID';

			$field['title_short']['kind'] = '';
			$field['title_short']['type'] = 'text';
			$field['title_short']['min'] = '1';
			$field['title_short']['max'] = '100';
			$field['title_short']['info'] = '请传递短标题';

			$field['title_short']['kind'] = '';
			$field['title_full']['type'] = 'text';
			$field['title_full']['min'] = '1';
			$field['title_full']['max'] = '255';
			$field['title_full']['info'] = '请传递长标题';

			$field['keyword']['kind'] = '';
			$field['keyword']['type'] = 'text';
			$field['keyword']['min'] = '1';
			$field['keyword']['max'] = '255';
			$field['keyword']['info'] = '请传递关键词';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				if ( $this->is_unique( $data['cate_id'], $data['title_short'] ) )
				{
					//上传预览图
					$templateurl = config( 'dir.physical_upload' );
					$FILE_NAME_STYLE = 'datetime';
					$upload = load( 'upload' );
					$upload_config = array( 'DEST_DIR' => $templateurl, 'FILE_TYPE' => 'jpg,png,gif',  'MUST_UPLOAD_FILE_INDEX' => 0 );
					$upload->init_cfg( array( 'preview_img_path' =>  $upload_config, 'photo' => $upload_config ) );
					$upfile = $upload->do_upload();

					//构造添加到html表的数据
					$html_data = array();
					if ( isset( $upfile['preview_img_path'] ) && count( $upfile['preview_img_path'] ) > 0 )
					{
						$html_data['preview_img_path'] = substr( $upfile['preview_img_path'][0], strlen( config( 'dir.tpl.root' ) ) - 1 );
					}
					$html_data['cate_id'] = $data['cate_id'];
					$html_data['title_short'] = $data['title_short'];
					$html_data['title_full'] = $data['title_full'];
					$html_data['title_short_color'] = $data['title_short_color'];
					$html_data['keyword'] = $data['keyword'];
					$html_data['keyword'] = $data['keyword'];
					$html_data['fake_click'] = $data['fake_click'];
					$html_data['home_promote_flag'] = ( isset( $data['home_promote_flag'] ) && $data['home_promote_flag'] == 'on' ? 1 : 0 );
					$html_data['home_order_num'] = $data['home_order_num'];
					$html_data['node_promote_flag'] = ( isset( $data['node_promote_flag'] ) && $data['node_promote_flag'] == 'on'  ? 1 : 0 );
					$html_data['node_order_num'] = $data['node_order_num'];
					$html_data['list_promote_flag'] = ( isset( $data['list_promote_flag'] ) && $data['list_promote_flag'] == 'on'  ? 1 : 0 );
					$html_data['list_order_num'] = $data['list_order_num'];
					$html_data['desc'] = $data['desc'];
					$html_data['create_time'] = time();
					$html_data['create_account_id'] = $_SESSION['user_info']['account_id'];
					//判断栏目内容添加时是否要审核
					$cate = load( 'biz.cate' );
					$cate_data = $cate->get( array( 'id'=>$data['cate_id'] ) );
					$html_data['audit_flag'] = $cate_data['audit_flag'] == 0 ? 1 : 0;

					$this->database->start();
					$id = $this->database->add( 'html', $html_data );

					if ( preg_match( '!^\d+$!', $id ) && $this->database->end() )
					{
						$data['id'] = $id;
						$data['upload_files'] = $upfile;
						//判断栏目数据模型
						$argument = load( 'biz.argument' );
						switch ( $cate_data['module_type_id'] )
						{
							case $argument->CM_NEWS:
								$this->add_news( $data );
								break;

							case $argument->CM_PHOTO:
								$this->add_photo( $data );
								break;

							case $argument->CM_VIDEO:
								$this->add_video( $data );
								break;

								default:
									$this->add_news( $data );
						}

						$result['success'] = true;
						$result['data']['id'] = $id;
					}
					else
					{
						$result['message'][] = '执行创建失败';
					}
				}
				else
				{
					$result['message'][] = '该栏目下存在相同记录。';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}


		/**
		 * 添加新闻内容
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function add_news( $data )
		{
			$new_data['html_id'] = $data['id'];
			$new_data['referer'] = $data['referer'];
			$new_data['author'] = $data['author'];
			$new_data['editor'] = $data['editor'];
			//$new_data['content'] = nl2br( $data['content'] );
			$new_data['content'] = $data['content'];	//wysiwyg mode

			$id = $this->database->add( 'html_news', $new_data );
		}


		/**
		 * 修改新闻内容
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function set_news( $data )
		{
			//$new_data['html_id'] = $data['id'];
			$new_data['referer'] = $data['referer'];
			$new_data['author'] = $data['author'];
			$new_data['editor'] = $data['editor'];
			//$new_data['content'] = preg_replace( '/([^>])\r\n/', '\\1<br />\r\n', $data['content'] );
			$new_data['content'] = $data['content'];	//wysiwyg mode

			$id = $this->database->set( 'html_news', $new_data, array( 'html_id' => $data['id'] ) );
		}


		/**
		 * 添加相册内容
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function add_photo( $data )
		{
			if ( isset( $data['upload_files']['photo'] ) && count( $data['upload_files']['photo'] ) > 0 )
			{
				$new_data['photo'] = substr( $data['upload_files']['photo'][0], strlen( config( 'dir.tpl.root' ) ) - 1 );
				//判断是否要加水印
				$argument = load( 'biz.argument' );
				$tmp = $argument->all( array( 'size' => 9999 ) );
				$tmp = $tmp['data'];
				$is_add_watermark = false;
				foreach ( $tmp as $index => $value )
				{
					if ( $value['ename'] == 'watermark' && $value['value'] == 1 )
					{
						$is_add_watermark = true;
						break;
					}
				}

				$arguemnt_field = array();
				if ( $is_add_watermark )
				{
					foreach ( $tmp as $index => $value )
					{
						$arguemnt_field[$value['ename']] = $value['value'];
					}

					$upload = load( 'upload' );
					if ( isset( $arguemnt_field['watermark_file'] ) && ! empty( $arguemnt_field['watermark_file'] ) )
					{
						$ok = $upload->add_watermark( $data['upload_files']['photo'][0], config( 'dir.root' ) . $arguemnt_field['watermark_file'], $data['upload_files']['photo'][0], $arguemnt_field['watermark_loc'], $arguemnt_field['watermark_transparent'] );

						if ( ! $ok['success'] )
						{
							//加水印失败
						}
					}
				}
			}

			$new_data['html_id'] = $data['id'];

			$id = $this->database->add( 'html_photo', $new_data );
		}


		/**
		 * 修改相册内容
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function set_photo( $data )
		{
			$new_data['html_id'] = $data['id'];

			if ( isset( $data['upload_files']['photo'] ) && count( $data['upload_files']['photo'] ) > 0 )
			{
				$new_data['photo'] = substr( $data['upload_files']['photo'][0], strlen( config( 'dir.tpl.root' ) ) - 1 );

				//判断是否要加水印
				$argument = load( 'biz.argument' );
				$tmp = $argument->all( array( 'size' => 9999 ) );
				$tmp = $tmp['data'];
				$is_add_watermark = false;
				foreach ( $tmp as $index => $value )
				{
					if ( $value['ename'] == 'watermark' && $value['value'] == 1 )
					{
						$is_add_watermark = true;
						break;
					}
				}

				$arguemnt_field = array();
				if ( $is_add_watermark )
				{
					foreach ( $tmp as $index => $value )
					{
						$arguemnt_field[$value['ename']] = $value['value'];
					}

					$upload = load( 'upload' );
					if ( isset( $arguemnt_field['watermark_file'] ) && ! empty( $arguemnt_field['watermark_file'] ) )
					{
						$ok = $upload->add_watermark( $data['upload_files']['photo'][0], config( 'dir.root' ) . $arguemnt_field['watermark_file'], $data['upload_files']['photo'][0], $arguemnt_field['watermark_loc'], $arguemnt_field['watermark_transparent'] );

						if ( ! $ok['success'] )
						{
							//加水印失败
						}
					}
				}
			}

			$id = $this->database->set( 'html_photo', $new_data, array( 'html_id' => $new_data['html_id'] ) );
		}


		/**
		 * 添加视频内容
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function add_video( $data )
		{
			$new_data['html_id'] = $data['id'];
			if ( isset( $data['upload_files']['video'] ) && count( $data['upload_files']['video'] ) > 0 )
			{
				$new_data['video'] = substr( $data['upload_files']['video'][0], strlen( config( 'dir.root' ) ) - 1 );
			}

			$id = $this->database->add( 'html_video', $new_data );
		}

		/**
		 * 修改视频内容
		 * detail description
		 * @param string user
		 * @param integer id
		 * @return int|string|array|object|mixed
		 */
		function set_video( $data )
		{
			$new_data['html_id'] = $data['id'];
			if ( isset( $data['upload_files']['video'] ) && count( $data['upload_files']['video'] ) > 0 )
			{
				$new_data['video'] = substr( $data['upload_files']['video'][0], strlen( config( 'dir.root' ) ) - 1 );
			}

			$id = $this->database->set( 'html_video', $new_data, array( 'html_id' => $new_data['html_id'] ) );
		}

		//删除
		function del( $data )
		{
			$result = array( 'success' => false, 'message' => array( '' ) );
			if ( isset( $data['ck_id'] ) && is_array( $data['ck_id'] ) )
			{
				$data['id_list'] = $data['ck_id'];
			}
			else
			{
				$data['id_list'][] = $data['id'];
				$data['on'] = 1;
			}
			foreach ( $data['id_list'] as $index => $id )
			{
				if ( preg_match( '!^\d+$!', $id ) )
				{
					$tmp = $this->get( array( 'id' => $id ) );
					if ( ! empty( $tmp ) )
					{
						$effected = $this->database->command( 'update html set status=' . $data['on'] . ' where id = ' . $id );
						if ( $effected > 0 )
						{
							//删除htm文件
							$path = config( 'dir.root' ) . $tmp['save_path'];
							if ( ! empty( $tmp['save_path'] ) && is_file( $path ) && is_writable( $path ) )
							{
								unlink( $path );
							}
							$result['success'] = true;
						}
					}
					else
					{
						$result['message'][] = '未找到记录';
					}
				}
			}
			return $result;
		}

		//批量审核
		function audit( $data )
		{
			$result = array( 'success' => false, 'message' => array( '' ) );

			if ( isset( $data['ck_id'] ) && is_array( $data['ck_id'] ) )
			{
				$data['id_list'] = $data['ck_id'];
			}
			else
			{
				$data['id_list'][] = $data['id'];
			}
			foreach ( $data['id_list'] as $index => $id )
			{
				if ( preg_match( '!^\d+$!', $id ) )
				{
					$effected = $this->database->command( 'update html set audit_flag=' . $data['on'] . ' where id = ' . $id );
					if ( $effected > 0 )
					{
						$result['success'] = true;
					}
				}
			}

			return $result;
		}


		//批量推荐
		function promote( $data )
		{
			$result = array( 'success' => false, 'message' => array( '' ) );

			if ( isset( $data['ck_id'] ) && is_array( $data['ck_id'] ) )
			{
				$data['id_list'] = $data['ck_id'];
			}
			else
			{
				$data['id_list'][] = $data['id'];
			}
			switch( $data['target'] )
			{
				case 'home':
					$target_field = 'home_promote_flag';
					break;

				case 'node':
					$target_field = 'node_promote_flag';
					break;

				case 'list':
					$target_field = 'list_promote_flag';
					break;

				default:
					$result['message'][] = '参数错误';
					return $result;
			}
			foreach ( $data['id_list'] as $index => $id )
			{
				if ( preg_match( '!^\d+$!', $id ) )
				{
					$effected = $this->database->command( 'update html set ' . $target_field . '=' . $data['on'] . ' where id = ' . $id );
					if ( $effected > 0 )
					{
						$result['success'] = true;
					}
				}
			}

			return $result;
		}


		//记录是否唯一
		function is_unique( $cate_id, $title_short )
		{
			$result = $this->database->unique( "select id from html where cate_id = " . $cate_id . " and title_short='" . $title_short . "' and status=0 " );
			return isset( $result['id'] ) ? false : true;
		}

		/**
		 * 更新文件存储路径
		 * detail description
		 * @param string html_id
		 * @param string path
		 * @return boolean
		 */
		function update_save_path( $html_id, $path  )
		{
			if ( empty( $path ) || empty( $html_id ) )
			{
				return false;
			}
			if ( ! is_numeric( $html_id ) )
			{
				return false;
			}

			$sql = "update html set save_path = '" . $path . "' where id=" . $html_id;
			$ok = $this->database->command( $sql);

			return $ok;
		}


	}
?>